package com.tjl.yangxixis.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ajguan.library.EasyRefreshLayout;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.activitys.SingeDetectionActivity;
import com.lyp.activitys.SingeIndoorActivity;
import com.lyp.adapters.SingeAdapter;
import com.lyp.adapters.SingeCarAdapter;
import com.lyp.adapters.SingeIndoorAdapter;
import com.lyp.jsonbean.JLCarBean;
import com.lyp.jsonbean.JLSingeIndoorBean;
import com.lyp.jsonbean.JlSingeDetectionBean;
import com.lyp.jsonbean.LoginBean;
import com.tjl.yangxixis.OriginalFragment;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.activity.MainActivity;
import com.tjl.yangxixis.activity.SingeDetailsActivity;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * @author Administrator
 *	销售经理的抢单界面
 */
public class SingeFragment extends OriginalFragment {

	View v;
	private RecyclerView mRecyclerView;
	private LinearLayoutManager mLinearLayoutManager;
	private SingeAdapter mSingeAdapter; //检测
	private SingeCarAdapter mSingeCarAdapter;//车内
	private SingeIndoorAdapter mSingIndoorAdapter;//室内
	private List<JlSingeDetectionBean.DataBean> mList = new ArrayList<>();//检测
	private List<JLCarBean.DataBean> mCarList = new ArrayList<>();//车内
	private List<JLSingeIndoorBean.DataBean> mIndoorList = new ArrayList<>();//室内
	private LoginBean.DataBean bean;
	private TextView mTitle;
	private EasyRefreshLayout mEasyRefreshLayout;

	private int mPosition;

	private int mRefrshPageNumber = 0;
	private int mLoadMorePageNumber = -1;

	@Override
	protected void lazyLoad() {

	}

	public View createView(LayoutInflater inflater, ViewGroup container,
						   Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v= inflater.inflate(R.layout.fragment_singe, null,true);
		init();
		return v;
	}
	private void init(){
		mTitle = (TextView) v.findViewById(R.id.singe_title);
		mRecyclerView= (RecyclerView) v.findViewById(R.id.sing_grid_recycler);
		mLinearLayoutManager = new LinearLayoutManager(getActivity());
		mRecyclerView.setLayoutManager(mLinearLayoutManager);
		bean = ((MainActivity)getActivity()).dataBean;

		mSingeAdapter = new SingeAdapter(mList,getActivity(),bean);
		mSingeCarAdapter = new SingeCarAdapter(mCarList,getActivity(),bean);
		mSingIndoorAdapter = new SingeIndoorAdapter(mIndoorList,getActivity(),bean);
		LogUtils.i("tag",mCarList.size()+"___rewrew");
		try {
			//检测
			if (bean.getServer_select().equals("第三方检测服务")){
				mRecyclerView.setAdapter(mSingeAdapter);
				SingeDetectionRefresh(bean.getServer_select(),mRefrshPageNumber,bean.getC_id());
				mSingeAdapter.setOnItemClickListener(new SingeAdapter.MyItemClickListener() {
					@Override
					public void onItemClick(View v, int position) {
						Intent intent = new Intent(getActivity(),SingeDetectionActivity.class);
						Bundle bundle = new Bundle();
						bundle.putSerializable("bean",bean);
						bundle.putSerializable("databean",mList.get(position));
						mPosition = position;
						intent.putExtras(bundle);
						startActivityForResult(intent, 0x100);
					}
				});
			}
			//车内
			else if (bean.getServer_select().equals("车内空气净化服务")) {
				mRecyclerView.setAdapter(mSingeCarAdapter);
				SingeCarRefresh(bean.getC_id(),bean.getServer_select(),mRefrshPageNumber);
				mSingeCarAdapter.setOnItemClickListener(new SingeCarAdapter.MyItemClickListener() {
					@Override
					public void onItemClick(View v, int position) {
						Intent intentcar = new Intent(getActivity(),SingeDetailsActivity.class);
						Bundle bundle = new Bundle();
						bundle.putSerializable("databean",mCarList.get(position));
						bundle.putSerializable("bean",bean);
						mPosition = position;
						intentcar.putExtras(bundle);
						startActivityForResult(intentcar, 0x200);
					}
				});
			}
			//室内
			else if (bean.getServer_select().equals("室内空气净化服务")){
				mRecyclerView.setAdapter(mSingIndoorAdapter);
				SingeIndoorRefresh(bean.getServer_select(),mRefrshPageNumber,bean.getC_id());
				mSingIndoorAdapter.setOnItemClickListener(new SingeIndoorAdapter.MyItemClickListener() {
					@Override
					public void onItemClick(View v, int position) {
						Intent intentindoor = new Intent(getActivity(),SingeIndoorActivity.class);
						Bundle bundle = new Bundle();
						bundle.putSerializable("databean",mIndoorList.get(position));
						bundle.putSerializable("bean",bean);
						mPosition = position;
						intentindoor.putExtras(bundle);
						startActivityForResult(intentindoor, 0x300);
					}
				});
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		mEasyRefreshLayout = (EasyRefreshLayout) v.findViewById(R.id.easylayout_singe);
							mEasyRefreshLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
								@Override
								public void onLoadMore() {
									new Thread(new Runnable() {
										@Override
										public void run() {
											try {
							if (bean.getServer_select().equals("第三方检测服务")) {
								SingeDetectionLoadMore(bean.getServer_select(), mLoadMorePageNumber, bean.getC_id());
							}else if (bean.getServer_select().equals("车内空气净化服务")) {
								SingeCarLoadMore(bean.getC_id(), bean.getServer_select(), mLoadMorePageNumber);
							}else if (bean.getServer_select().equals("室内空气净化服务")) {
								SingeIndoorLoadMore(bean.getServer_select(), mLoadMorePageNumber, bean.getC_id());
							}
						} catch (IOException e) {
							e.printStackTrace();
						}
						getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								mEasyRefreshLayout.loadMoreComplete();
							}
						});
					}
				}).start();
			}

			@Override
			public void onRefreshing() {
				new Thread(new Runnable() {
					@Override
					public void run() {
						SystemClock.sleep(1000);
						try {
							if (bean.getServer_select().equals("第三方检测服务")) {
								SingeDetectionRefresh(bean.getServer_select(), mRefrshPageNumber, bean.getC_id());
							}else if (bean.getServer_select().equals("车内空气净化服务")) {
								SingeCarRefresh(bean.getC_id(), bean.getServer_select(), mRefrshPageNumber);
							}
							else if (bean.getServer_select().equals("室内空气净化服务")) {
								SingeIndoorRefresh(bean.getServer_select(), mRefrshPageNumber, bean.getC_id());
							}
						} catch (IOException e) {
							e.printStackTrace();
						}
						getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								mEasyRefreshLayout.refreshComplete();
							}
						});
					}
				}).start();
			}
		});
	}

	//第三方检测/刷新
	public void SingeDetectionRefresh(String server_select,int page,String c_id) throws IOException{
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
		YangxixiApi github = retrofit.create(YangxixiApi.class);
		Call<JlSingeDetectionBean> call = github.getSingDetection(server_select,page,c_id);
		call.enqueue(new Callback<JlSingeDetectionBean>() {
			@Override
			public void onResponse(Call<JlSingeDetectionBean> call, final Response<JlSingeDetectionBean> response) {
				if (response.body().getResult()== 1 ) {
					getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (response.body().getNext_page() > 0) {
								mList.addAll(0, response.body().getData());
								mSingeAdapter.notifyDataSetChanged();
								mRefrshPageNumber++;
							} else {
								ToastUtils.showShort(response.body().getMessage());
							}
						}
					});
				}else {
					ToastUtils.showShort(response.body().getMessage());
				}
			}
			@Override
			public void onFailure(Call<JlSingeDetectionBean> call, Throwable t) {
				ToastUtils.showShort(t.getMessage());
			}
		});
	}

	//第三方检测/加载
	public void SingeDetectionLoadMore(String server_select,int page,String c_id) throws IOException{
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
		YangxixiApi github = retrofit.create(YangxixiApi.class);
		Call<JlSingeDetectionBean> call = github.getSingDetection(server_select,page,c_id);
		call.enqueue(new Callback<JlSingeDetectionBean>() {
			@Override
			public void onResponse(Call<JlSingeDetectionBean> call, final Response<JlSingeDetectionBean> response) {
				if (response.body().getResult()== 1 ) {
					getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (mLoadMorePageNumber >= 0) {
								mList.addAll(response.body().getData());
								mSingeAdapter.notifyDataSetChanged();
								mLoadMorePageNumber--;
							} else {
								Toast.makeText(getActivity(),"没有更多数据了",Toast.LENGTH_SHORT).show();
							}
						}
					});
				}else {
					ToastUtils.showShort(response.body().getMessage());
				}
			}
			@Override
			public void onFailure(Call<JlSingeDetectionBean> call, Throwable t) {
				ToastUtils.showShort(t.getMessage());
			}
		});
	}
	//车内/刷新
	public void SingeCarRefresh(String c_id,String server_select,int page) throws IOException{
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
		YangxixiApi github = retrofit.create(YangxixiApi.class);
		Call<JLCarBean> call = github.getSingCar(c_id,server_select,page);
		call.enqueue(new Callback<JLCarBean>() {
			@Override
			public void onResponse(Call<JLCarBean> call, final Response<JLCarBean> response) {
				if (response.body().getResult()== 1 ) {
					getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							LogUtils.i("tag",mCarList.size()+"____");
							if (response.body().getData().size() > 0) {
								mCarList.addAll(0, response.body().getData());
								mSingeCarAdapter.notifyDataSetChanged();
								mRefrshPageNumber++;
							} else {
								ToastUtils.showShort("没有更多数据");
							}
						}
					});
				}else {
					ToastUtils.showShort(response.body().getMessage());
				}
			}
			@Override
			public void onFailure(Call<JLCarBean> call, Throwable t) {
				ToastUtils.showShort(t.getMessage());
			}
		});
	}

	//车内/加载
	public void SingeCarLoadMore(String c_id,String server_select,int page) throws IOException{
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
		YangxixiApi github = retrofit.create(YangxixiApi.class);
		Call<JLCarBean> call = github.getSingCar(c_id,server_select,page);
		call.enqueue(new Callback<JLCarBean>() {
			@Override
			public void onResponse(Call<JLCarBean> call, final Response<JLCarBean> response) {
				if (response.body().getResult()== 1 ) {
					getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (mLoadMorePageNumber >= 0) {
								mCarList.addAll(response.body().getData());
								mSingeCarAdapter.notifyDataSetChanged();
								mLoadMorePageNumber--;
							} else {
								Toast.makeText(getActivity(),"没有更多数据了",Toast.LENGTH_SHORT).show();
							}
						}
					});
				}else {
					ToastUtils.showShort(response.body().getMessage());
				}
			}
			@Override
			public void onFailure(Call<JLCarBean> call, Throwable t) {
				ToastUtils.showShort(t.getMessage());
			}
		});
	}
	//室内/刷新
	public void SingeIndoorRefresh(String server_select,int page,String c_id) throws IOException{
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
		YangxixiApi github = retrofit.create(YangxixiApi.class);
		Call<JLSingeIndoorBean> call = github.getSingIndoor(server_select,page,c_id);
		call.enqueue(new Callback<JLSingeIndoorBean>() {
			@Override
			public void onResponse(Call<JLSingeIndoorBean> call, final Response<JLSingeIndoorBean> response) {
				if (response.body().getResult()== 1 ) {
					getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (response.body().getNext_page() > 0) {
								mIndoorList.addAll(0, response.body().getData());
								mSingIndoorAdapter.notifyDataSetChanged();
								mRefrshPageNumber++;
							} else {
								Toast.makeText(getActivity(),"没有更多数据了",Toast.LENGTH_SHORT).show();
							}
						}
					});
				}else {
					ToastUtils.showShort(response.body().getMessage());
				}
			}
			@Override
			public void onFailure(Call<JLSingeIndoorBean> call, Throwable t) {
				ToastUtils.showShort(t.getMessage());
			}
		});
	}

	//室内/加载
	public void SingeIndoorLoadMore(String server_select,int page,String c_id) throws IOException{
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
		YangxixiApi github = retrofit.create(YangxixiApi.class);
		Call<JLSingeIndoorBean> call = github.getSingIndoor(server_select,page,c_id);
		call.enqueue(new Callback<JLSingeIndoorBean>() {
			@Override
			public void onResponse(Call<JLSingeIndoorBean> call, final Response<JLSingeIndoorBean> response) {
				if (response.body().getResult()== 1 ) {
					getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (mLoadMorePageNumber >= 0) {
								mIndoorList.addAll(response.body().getData());
								mSingIndoorAdapter.notifyDataSetChanged();
								mLoadMorePageNumber--;
							} else {
								Toast.makeText(getActivity(),"没有更多数据了",Toast.LENGTH_SHORT).show();
							}
						}
					});
				}else {
					ToastUtils.showShort(response.body().getMessage());
				}
			}
			@Override
			public void onFailure(Call<JLSingeIndoorBean> call, Throwable t) {
				ToastUtils.showShort(t.getMessage());
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == -1) { // 等价于 Activity.RESULT_OK
			switch (requestCode) {
				case 0x100:
					mList.remove(mPosition);
					mSingeAdapter.notifyDataSetChanged();
					break;
				case 0x200:
					mCarList.remove(mPosition);
					mSingeCarAdapter.notifyDataSetChanged();
					break;
				case 0x300:
					mIndoorList.remove(mPosition);
					mSingIndoorAdapter.notifyDataSetChanged();
					break;
			}

		}
	}
}
